package com.openflag.clientservice.controller;


import com.openflag.clientservice.domain.*;
import com.openflag.clientservice.repository.ClientRepository;
import com.openflag.clientservice.service.ClientService;
import jdk.jfr.Registered;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.openflag.clientservice.util.ResultUtil;
import java.util.Optional;

import static com.openflag.clientservice.domain.ResultStatus.*;
import static com.openflag.clientservice.util.ResultUtil.*;


//实现类，客户端入口，返回Rest消息
@RestController
@CrossOrigin(origins = "*")
public class ClientController {
    //注入实体类,让Spring去ClientService实现的类里找可以用的。
    @Autowired
    private ClientService clientService;

    @Autowired
    private ClientRepository clientRepository;

    private Logger LOG = LoggerFactory.getLogger(ClientController.class);


    @GetMapping("/getClient/{id}")
    //从路径传进来的变量，则用这个注解  localhost:8087/getClient/1
    public GetClientUnit getClient(@PathVariable Long id){
        return clientService.getClient(id);
    }
    @GetMapping("/getPosition/{id}")
    public Integer getPosition(@PathVariable Long id){
        return clientRepository.getPosition(id);
    }

    @PostMapping("/login")
    public Result<Client> login(@RequestBody LoginRequest loginRequest){

        String account =loginRequest.getAccount();
        String password = loginRequest.getPassword();
        Optional<Client> optionalClient= clientService.loginClient(account,password);
        if (optionalClient.isPresent()){
            Result<Client> result= new Result<Client>();
            optionalClient.get().setPassword(" ");
            result = success(optionalClient.get());
            return result;
        }


        return fail(USER_NOT_FOUND);
    }


    // 输入注册信息，返回状态码，0是创建成功，1是用户名重名，2是用户名不符合规则，
    // 3是密码不符合规则，4是身份验证失败,5姓名不符合规则。
    @PostMapping("/register")
    public Result<Client> register(@RequestBody RegisterRequest registerRequest){

        String name = registerRequest.getName();
        String account = registerRequest.getAccount();
        String password = registerRequest.getPassword();
        Integer position = registerRequest.getPosition();

        Integer status = 0;

        status =  clientService.registerClient(name,account,password,position);

        Result<Client> feedback = null;

        switch (status){
            case 0:
                feedback = success(null);
                break;
            case 1:
                feedback = fail(REGISTER_SAME_ACCOUNT);
                break;
            case 2:
                feedback = fail(REGISTER_WRONG_ACCOUNT);
                break;
            case 3:
                feedback = fail(REGISTER_WRONG_PASSWORD);
                break;

            case 4:
                feedback = fail(REGISTER_WRONG_POSITION);
                break;

            case 5:
                feedback = fail(REGISTER_WRONG_NAME);
                break;
        }

        return feedback;

    }

    @GetMapping("/position/{clientId}")
    public Integer Position(@PathVariable Long clientId){
        Integer position = clientRepository.getPosition(clientId);


        return position;

    }



}
